local function DoHook() -- 非常不好的一个钩子
	local old_PopulateWorld = PopulateWorld
	local old_SpawnPrefab   = SpawnPrefab
	function GLOBAL.PopulateWorld(savedata, profile, playercharacter, playersavedataoverride, ...)
		GLOBAL.SpawnPrefab = function(name, ...)
			if name == "forest" then
				print("[RG] Reparam - SpawnPrefab()")
				local id = savedata.meta and savedata.meta.level_id
				if id ~= nil and RG_LEVELS_INDEX[id] ~= nil then 
					name = string.lower(id)
				end
			end

			return old_SpawnPrefab(name, ...)
		end

		local wilson = old_PopulateWorld(savedata, profile, playercharacter, playersavedataoverride, ...)

		GLOBAL.SpawnPrefab = old_SpawnPrefab

		return wilson
	end

	local old_DoInitGame = DoInitGame
	function GLOBAL.DoInitGame(...)
		old_DoInitGame(...)

		if GetWorld().components.rg_worldtitle then
			GetWorld().components.rg_worldtitle:OnGameInit()
		end
	end
end

local old_OnGenerateNewWorld = SaveIndex.OnGenerateNewWorld
function SaveIndex:OnGenerateNewWorld(...)
	DoHook()
	return old_OnGenerateNewWorld(self, ...)
end

for k,v in pairs(RG_LEVELS)do
	table.insert(PrefabFiles, k:lower())
end

AddSimPostInit(function()
	if GetWorld().prefab == "rg_start" then
		TheCamera:SetHeadingTarget(-90)
		TheCamera:Snap()
	end
end)



RG_AddPrefabPostInit("chester_eyebone", function(inst)
	inst:DoTaskInTime(0, inst.Remove)
end)

RG_AddPrefabPostInit("wormhole", function(inst)
	print("[RG] Force remove wormhole")
	inst:DoTaskInTime(0, inst.Remove)
end)

-- dst component
function EntityScript:AddDSTComponent(name)
	self:AddComponent("dst/"..name)
	self.components[name] = self.components["dst/"..name]
end

-- 003/standardcomponents.lua
local old_world = GetWorldCollision
if old_world ~= nil then
	function GLOBAL.GetWorldCollision(...)
		if string.match(GetWorld().prefab, "^rg_") then
			return COLLISION.WORLD_01
		else
			return old_world(...)
		end
	end
end

local old_water = GetWaterCollision
if old_water ~= nil then
	function GLOBAL.GetWaterCollision(...)
		if string.match(GetWorld().prefab, "^rg_") then
			return COLLISION.WORLD_01 -- porkland = WORLD_02
		else
			return old_water(...)
		end
	end
end
